package com.sdkj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdkj.mapper.SetmealCheckgroupMapper;
import com.sdkj.mapper.SetmealMapper;
import com.sdkj.pojo.Setmeal;
import com.sdkj.pojo.SetmealCheckgroup;
import com.sdkj.service.SetmealService;
import com.sdkj.util.PageResult;
import com.sdkj.util.QueryPageBean;
import com.sdkj.util.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Resource
    private SetmealMapper setmealMapper;
    @Resource
    private SetmealCheckgroupMapper setmealCheckgroupMapper;
    @Override
    public boolean addSetmealInfo(Integer[] ids, Setmeal setmeal) {
        //添加套餐数据
        boolean flag = setmealMapper.insert(setmeal)>0;
        //添加关系数据
        if (ids!=null && ids.length>0){
            for (Integer id : ids) {
                SetmealCheckgroup setmealCheckgroup = new SetmealCheckgroup();
                setmealCheckgroup.setCheckgroupId(id);
                setmealCheckgroup.setSetmealId(setmeal.getId());
                flag = setmealCheckgroupMapper.insert(setmealCheckgroup)>0;
            }
        }
        return flag;
    }
    @Override
    public PageResult findPageInfo(QueryPageBean queryPageBean) {
        String queryString = queryPageBean.getQueryString();
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (queryString!=null && queryString.length()>0){
            lambdaQueryWrapper.like(Setmeal::getCode,queryString);
            lambdaQueryWrapper.or();
            lambdaQueryWrapper.like(Setmeal::getName,queryString);
            lambdaQueryWrapper.or();
            lambdaQueryWrapper.like(Setmeal::getHelpCode,queryString);
        }
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        Page<Setmeal> page = new Page<>(currentPage,pageSize);

        Page<Setmeal> newPage = setmealMapper.selectPage(page, lambdaQueryWrapper);
        long total = newPage.getTotal();
        List<Setmeal> records = newPage.getRecords();
        PageResult pageResult = new PageResult(total,records);
        return pageResult;
    }

    @Override
    public boolean deleteInfoById(Integer id)
    {
        //删除中间表
        LambdaQueryWrapper<SetmealCheckgroup> lambdaQueryWrapper  = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SetmealCheckgroup::getSetmealId,id);
        boolean flag = setmealCheckgroupMapper.delete(lambdaQueryWrapper)>0;

        //删除套餐的配套图片(磁盘)
        Setmeal setmeal = setmealMapper.selectById(id);

        String img = setmeal.getImg(); //图片的名称
        String path = "F:/j_demo/photos/";
        File file = new File(path+img);
        flag = file.delete();
        //删除套餐数据
        flag = setmealMapper.deleteById(id)>0;
        return flag;
    }

    @Override
    public Result getGroupIds(Integer id) {
        List<Integer> list = setmealCheckgroupMapper.getGroupIdBySetmealId(id);
        return new Result(true,"",list);
    }
    //更新套餐信息
    @Override
    public boolean updateSetmealInfo(Integer[] ids, Setmeal setmeal) {
        //更新套餐表数
        boolean flag  = setmealMapper.updateById(setmeal)>0;
        //删除关系表数据
        LambdaQueryWrapper<SetmealCheckgroup> lambdaQueryWrapper  = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SetmealCheckgroup::getSetmealId,setmeal.getId());
        flag = setmealCheckgroupMapper.delete(lambdaQueryWrapper)>0;
        System.out.println("flag =======" + flag);
        //添加新的关系数据
        if (ids!=null && ids.length>0){
            for (Integer id : ids) {
                SetmealCheckgroup setmealCheckgroup = new SetmealCheckgroup();
                setmealCheckgroup.setCheckgroupId(id);
                setmealCheckgroup.setSetmealId(setmeal.getId());
                flag = setmealCheckgroupMapper.insert(setmealCheckgroup)>0;
            }
        }
        return flag;
    }

}
